using System;
using System.Collections.Generic;

namespace RTMP.SimpleClient.Messages
{
    /// <summary>
    /// 基础RTMP消息类
    /// </summary>
    public abstract class RTMPMessage
    {
        /// <summary>
        /// 时间戳
        /// </summary>
        public uint Timestamp { get; set; }

        /// <summary>
        /// 流ID
        /// </summary>
        public uint StreamId { get; set; }

        /// <summary>
        /// 原始数据（调试用）
        /// </summary>
        public byte[]? RawData { get; set; }
    }

    /// <summary>
    /// RTMP命令消息
    /// </summary>
    public class RTMPCommandMessage : RTMPMessage
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public string CommandName { get; set; } = string.Empty;

        /// <summary>
        /// 事务ID
        /// </summary>
        public double TransactionId { get; set; }

        /// <summary>
        /// 命令对象
        /// </summary>
        public Dictionary<string, object>? CommandObject { get; set; }

        /// <summary>
        /// 附加信息
        /// </summary>
        public object? AdditionalInfo { get; set; }

        /// <summary>
        /// 是否为成功响应
        /// </summary>
        public bool IsSuccessResponse => CommandName == "_result";

        /// <summary>
        /// 是否为错误响应
        /// </summary>
        public bool IsErrorResponse => CommandName == "_error";
    }

    /// <summary>
    /// Connect命令响应
    /// </summary>
    public class ConnectResponseMessage : RTMPCommandMessage
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        public Dictionary<string, object>? ConnectionInfo { get; set; }

        /// <summary>
        /// 是否连接成功
        /// </summary>
        public bool IsConnected => IsSuccessResponse;
    }

    /// <summary>
    /// CreateStream命令响应
    /// </summary>
    public class CreateStreamResponseMessage : RTMPCommandMessage
    {
        /// <summary>
        /// 新创建的流ID
        /// </summary>
        public uint NewStreamId { get; set; }
    }

    /// <summary>
    /// Play命令状态消息
    /// </summary>
    public class PlayStatusMessage : RTMPMessage
    {
        /// <summary>
        /// 状态代码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 状态级别
        /// </summary>
        public string Level { get; set; } = string.Empty;

        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 详细信息
        /// </summary>
        public Dictionary<string, object>? Details { get; set; }
    }

    /// <summary>
    /// Publish命令状态消息
    /// </summary>
    public class PublishStatusMessage : RTMPMessage
    {
        /// <summary>
        /// 状态代码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 状态级别
        /// </summary>
        public string Level { get; set; } = string.Empty;

        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 流名称
        /// </summary>
        public string StreamName { get; set; } = string.Empty;

        /// <summary>
        /// 详细信息
        /// </summary>
        public Dictionary<string, object>? Details { get; set; }
    }

    /// <summary>
    /// 用户控制消息
    /// </summary>
    public class UserControlMessage : RTMPMessage
    {
        /// <summary>
        /// 事件类型
        /// </summary>
        public UserControlEventType EventType { get; set; }

        /// <summary>
        /// 事件数据
        /// </summary>
        public uint[]? EventData { get; set; }
    }

    /// <summary>
    /// 用户控制事件类型
    /// </summary>
    public enum UserControlEventType : ushort
    {
        StreamBegin = 0,
        StreamEOF = 1,
        StreamDry = 2,
        SetBufferLength = 3,
        StreamIsRecorded = 4,
        PingRequest = 6,
        PingResponse = 7
    }

    /// <summary>
    /// 音频数据消息
    /// </summary>
    public class AudioDataMessage : RTMPMessage
    {
        /// <summary>
        /// 音频数据
        /// </summary>
        public byte[] AudioData { get; set; } = Array.Empty<byte>();

        /// <summary>
        /// 音频格式信息
        /// </summary>
        public byte AudioFormat { get; set; }
    }

    /// <summary>
    /// 视频数据消息
    /// </summary>
    public class VideoDataMessage : RTMPMessage
    {
        /// <summary>
        /// 视频数据
        /// </summary>
        public byte[] VideoData { get; set; } = Array.Empty<byte>();

        /// <summary>
        /// 视频格式信息
        /// </summary>
        public byte VideoFormat { get; set; }

        /// <summary>
        /// 帧类型
        /// </summary>
        public VideoFrameType FrameType { get; set; }
    }

    /// <summary>
    /// 视频帧类型
    /// </summary>
    public enum VideoFrameType : byte
    {
        KeyFrame = 1,
        InterFrame = 2,
        DisposableInterFrame = 3,
        GeneratedKeyFrame = 4,
        VideoInfoFrame = 5
    }
}